package com.pethospital.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Data
@ToString
@TableName("appointment")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Appointment implements Serializable {
    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private Long userId;
    private Long petId;
    private Long staffId;
    private Long departmentId;
    private LocalDateTime appointmentTime;
    private Integer type; // 预约类型：0-普通就诊，1-疫苗接种，2-绝育手术，3-其他
    private String description; // 症状描述
    private String remark; // 备注
    private Integer status; // 状态：0-待确认，1-已确认，2-已完成，3-已取消
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    
    // 辅助字段，不存入数据库
    @TableField(exist = false)
    private String petName;
    
    @TableField(exist = false)
    private String staffName;
    
    @TableField(exist = false)
    private String departmentName;
    
    @TableField(exist = false)
    private Object review;
    
    // 非数据库字段，用于存储额外的信息（宠物名称、医生姓名、科室名称等）
    @TableField(exist = false)
    @JsonIgnore // 在JSON序列化时忽略这个字段
    private Map<String, Object> extraData = new HashMap<>();
    
    /**
     * 就诊记录ID
     */
    private Long medicalRecordId;
    
    /**
     * 设置额外数据
     * @param key 键
     * @param value 值
     */
    public void setExtra(String key, Object value) {
        if (extraData == null) {
            extraData = new HashMap<>();
        }
        extraData.put(key, value);
        
        // 同时设置对应的辅助字段
        if ("petName".equals(key) && value instanceof String) {
            this.petName = (String) value;
        } else if ("staffName".equals(key) && value instanceof String) {
            this.staffName = (String) value;
        } else if ("departmentName".equals(key) && value instanceof String) {
            this.departmentName = (String) value;
        } else if ("review".equals(key)) {
            this.review = value;
        }
    }
    
    /**
     * 获取额外数据
     * @param key 键
     * @return 值
     */
    public Object getExtra(String key) {
        if (extraData == null) {
            return null;
        }
        return extraData.get(key);
    }
    
    /**
     * 将额外数据作为顶级属性添加到JSON中
     * @return 额外数据
     */
    @JsonAnyGetter
    public Map<String, Object> getExtraData() {
        if (extraData == null) {
            return new HashMap<>();
        }
        return extraData;
    }
    
    /**
     * 允许从JSON反序列化时设置额外属性
     * @param key 属性名
     * @param value 属性值
     */
    @JsonAnySetter
    public void setExtraField(String key, Object value) {
        if (extraData == null) {
            extraData = new HashMap<>();
        }
        extraData.put(key, value);
        
        // 同时设置对应的辅助字段
        if ("petName".equals(key) && value instanceof String) {
            this.petName = (String) value;
        } else if ("staffName".equals(key) && value instanceof String) {
            this.staffName = (String) value;
        } else if ("departmentName".equals(key) && value instanceof String) {
            this.departmentName = (String) value;
        } else if ("review".equals(key)) {
            this.review = value;
        }
    }
} 